function OpenAddRoomTypes(){
	document.location.href ="/adminconsole/RoomsManager/RoomTypes/AddRoomTypes.jsp";
}

function OpenEditRoomTypes(id){
	document.location.href ="/adminconsole/RoomsManager/RoomTypes/AddRoomTypes.jsp?id="+id;
}

function AddRoomDialog(dom,floorid){
	var dialog = editAdminDataDialog("right",dom,"Add New Room","/adminconsole/RoomsManager/Rooms/AddRoom.jsp","addDialog",AddRoomEventClick,loadEditRoomElementFinish,floorid);
}

function loadEditRoomElementFinish(dialog,floorid){
	cursorDialogResize(dialog,{width:600});
	loadFilter(dialog,floorid);
}

function AddRoomEventClick(dialog,id){
	waitingDialog({});
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	
	 if(dialog.find("#searchByRoomType").val() == -1){
		 alert('chose room type');
		 return false;
	 }
	 if(dialog.find("#searchByFloor").val() == -1){
		 alert('chose floor');
		 return false;
	 }
	 if(dialog.find("#searchByBlock").val() == -1){
		 alert('chose block');
		 return false;
	 }
	 var roomname = dialog.find("#roomname").val();
	 var roomtype = dialog.find("#searchByRoomType").val();
	 var floor = dialog.find("#searchByFloor").val();
	 var block = dialog.find("#searchByBlock").val();
	 var description = $("#description").val();
	 var func = "addRoom";

	 var url = "/roomtypeService";
		var data = { func : func,
				roomname: roomname,
				roomtype: roomtype,
				floor: floor,
				block: block,
				description: description,
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
			}
		simpleservice(url,data,showRoomtypeList);
}

function showRoomtypes(id){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/roomtypeService";
	var data = { func: "getAllRoomtype4Admin",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showRoomtypeList);
}

function showRoomtypeList(data){	
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = objArray2["pageAmount"];
	var line = objArray2["lineAmount"];
	var pagePointer = objArray2["pageNumber"];
	var objArray = objArray2["data"];
	$("#listsize").html("&nbsp;("+objArray2["dataAmount"]+")");
	var lineNumber = (line * pagePointer) - (line-1) ;
	
	var roomamount = objArray2['roomamount'];

	if(objArray.length == 0){
		text = "<tr><td colspan=\"11\" style=\"border-top:1px #D6D7D5 solid;border-bottom:1px #D6D7D5 solid;\">"+nodata+"</td></tr>";
		sumtext += text;
	}else{
		for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> "+
	        	"<td id=\"col_1\" align=\"center\"> 1 </td>"+
	            "<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"id[]\" value=\""+objArray[i]['id']+"\"> </td>"+
	            "<td align=\"left\">Pic here</td>"+
	            "<td align=\"left\" >"+objArray[i]['shortName']+"</td>" +
	            "<td align=\"center\"> "+objArray[i]['StandardOccupancy']+" Persons  </td>"+
	            "<td align=\"center\"> "+objArray[i]['MaxOccupancy']+" Persons  </td>"+
				"<td align=\"center\"><a href=\"/adminconsole/RoomsManager/Rooms/Rooms.jsp?roomtypeid="+objArray[i]['id']+"\"><b>"+roomamount[i]+" Rooms</b></a>&nbsp;/&nbsp;<a href=\"#\" onclick=\"AddRoomDialog($(this),"+objArray[i]['id']+")\">Add a Room</a></td>"+
	            "<td align=\"center\">"+objArray[i]['extraBedAllowed']+"</td> "+
	            "<td align=\"center\"><a href=\"#\" onclick=\"OpenEditRoomTypes("+objArray[i]['id']+")\">Edit</a></td>"+
				"<td align=\"center\"><a href=\"#\" onclick=\"changeStatus("+objArray[i]['id']+") \"> " +
				"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>"+
				"</td>  "+
	            "<td align=\"center\">"+
				"	<input  type=\"text\" id=\"shortOrder_"+i+"\" onblur=\"EditSortOder(id,"+objArray[i]['id']+")\" name=\"shortOrder\" value=\""+objArray[i]['sortOrder']+"\" size=\"3\">"+
				"</td> "+
	        "</tr> ";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showRoomtypes()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showRoomtypes()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
	}
	$("#showList tr").not($("#showList").find('.tablehead').parent()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showRoomtypes(id)");
		$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showRoomtypes(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function moveAmenities(SS1,SS2)
{
    var SelID='';
    var SelText='';
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}
function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}

function loadAmenity(){
	var url = "/amenityService";
	var data = { func : "getShowAmenities"
		}
	simpleservice(url,data,setAmenityOption);
}

function setAmenityOption(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	for ( var i = 0; i < objArray.length; i++) {
		$("#mainAmenity").append(
				$("<option>").val(objArray[i]['id']).html(objArray[i]['title']));
		}
}

function loadTax(){
	var url = "/taxService";
	var data = { func : "getShowTax"
		}
	simpleservice(url,data,setShowTax);
}

function setShowTax(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	var text = "";
	var sumtext = "";
	var taxtype = "";
	for(var i=0;i<objArray.length;i++){
		if(objArray[i]['IsOnRoomTariff']){
			taxtype = "% On Room Tariff";
		}else{
			taxtype = "B Per Reservation";
		}
		text = "<tr>"+
	    "<td width=\"3%\" valign=\"top\"><input type=\"checkbox\" name=\"tax\" value=\""+objArray[i]['id']+"\"></td>"+
		"<td>"+objArray[i]['CurrencyValue']+taxtype+"</td>" +
		"</tr>";
		sumtext += text;
	}
	$("#taxList").append(sumtext);
}

function AddRoomtype(){
	var id = $("#tableform").attr("refid");
	if(id == null){
		var func = "addRoomtype";
	}else{
		var func = "editRoomtype"
	}
	var shortname = $("#shortname").val();
	var pic = $("#pic").val();
	var caption = $("#caption").val();
	var description = $("#description").val();
	var stdOcc= $("#stdOcc").val();
	var higherOcc = $("#higherOcc").val();
	var exBed = $("#exBed").val();
	var selectedAmenity= "";
	for(var i = 0;i<$("#selectedAmenity option").size();i++){
		if(i != $("#selectedAmenity option").size()-1){
			selectedAmenity += $($("#selectedAmenity option")[i]).val()+",";
		}else{
			selectedAmenity += $($("#selectedAmenity option")[i]).val();
		}
	}
	var basePrice = $("#basePrice").val();
	var higherPrice = $("#higherPrice").val();
	var exBedPrice = $("#exBedPrice").val();
	var tax = "";
	for(var i = 0;i<$("#taxList [type=checkbox]").size();i++){
		if($("#taxList [type=checkbox]")[i].checked == true){
			tax += $($("#taxList [type=checkbox]")[i]).val()+",";
		}
	}
	tax = tax.substring(0,tax.length-1);
	var url = "/roomtypeService";
	var data = { func : func,
				id : id,
				shortName : shortname,
				pic : pic,
				imageCaption : caption,
				description : description,
				StandardOccupancy :stdOcc,
				MaxOccupancy :higherOcc,
				extraBedAllowed :exBed,
				amenityListString :selectedAmenity,
				basePrice :basePrice,
				higherPrice :higherPrice,
				extraBedPrice :exBedPrice,
				applicableTaxListString :tax
		}
	simpleservice(url,data,addRoomtypeSuccess);
}
function addRoomtypeSuccess(data){
	document.location.href ="/adminconsole/RoomsManager/RoomTypes/RoomTypes.jsp";
}

function EditRoomTypes(id){
	if(id != null){
		var url = "/roomtypeService";
		var data = { func : "getRoomtypeById",
				id : id
			}
		simpleservice(url,data,editSuccess);
	}
}

function editSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#tableform").attr("refid",objArray[0]['id']);
	$("#shortname").val(objArray[0]['shortName']);
	$("#caption").val(objArray[0]['imageCaption']);
	$("#description").val(objArray[0]['description']);
	$("#stdOcc").val(objArray[0]['StandardOccupancy']);
	$("#higherOcc").val(objArray[0]['MaxOccupancy']);
	$("#exBed").val(objArray[0]['extraBedAllowed']);
	//selection
	$("#selectedAmenity option").remove();
	if(objArray[0]['amenityList'] != null){
		for ( var i = 0; i < objArray[0]['amenityList'].length; i++) {
			$("#selectedAmenity").append(
					$("<option>").val(objArray[0]['amenityList'][i]['id']).html(objArray[0]['amenityList'][i]['title']));
			}
	}
	$("#mainAmenity option").remove();
	for ( var i = 0; i < objArray[2].length; i++) {
		$("#mainAmenity").append(
				$("<option>").val(objArray[2][i]['id']).html(objArray[2][i]['title']));
		}
	$("#basePrice").val(objArray[0]['basePrice']);
	$("#higherPrice").val(objArray[0]['upchargeperson']);
	$("#exBedPrice").val(objArray[0]['extraBedPrice']);
	//checked box
	$("#roomamount").text(objArray[1]);
	for(var i=0;i<$("#taxList [type=checkbox]").size();i++){
		for(var k=0;k< $(objArray[0]['applicableTaxListString'].split(",")).size();k++){
			if($(objArray[0]['applicableTaxListString'].split(","))[k] == $($("#taxList [type=checkbox]")[i]).val()){
				$("#taxList [type=checkbox]")[i].checked = true;
			}
		}
	}
}

function EditSortOder(eleId,id){
	waitingDialog({});
	var sortOrder = $("#"+eleId).val();
	var url = "/roomtypeService";
	var data = { func : "editSortOrder",
			id : id,
			sortOrder: sortOrder
		}
	simpleservice(url,data,editSortSuccess);
}

function editSortSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(objArray != "T"){
		alert("try again");
	}
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteRoomTypes(){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).val()+",";
		}
	}
	var url = "/roomtypeService";
	var data = { func : "delRoomtype",
			roomtypeId : listCheckBox.substring(0,listCheckBox.length - 1),
			offset: (line*pagePointer)-line,
			line: line,
			pagePointer: pagePointer
		}
	simpleservice(url,data,DeleteSuccess);
}

function DeleteSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(objArray["data"] != null){
		showRoomtypeList(data);
	}else{
		var list = objArray["cantdelete"];
		var roomname = "";
		for(var i=0;i<list.length;i++){
			roomname += objArray["cantdelete"][i]+" ";
		}
		simpleDialog("Can't delete this room type ,Please move all reservation out from room type "+roomname);
		if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	}
}

function changeStatus(id){
	waitingDialog({});
	var url = "/roomtypeService";
	var data = { func : "changeRoomtype",
			roomtypeId : id
		}
	simpleservice(url,data,changeStatusSuccess);
}

function changeStatusSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(objArray["canchange"]){
		$("#status"+objArray["data"]["id"]).text(objArray["data"]["status"]);
	}else{
		simpleDialog("Can't Change this room type , Please move all reservation out from "+objArray["data"]["shortName"]);
	}
	
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function loadFilter(dialog,id){
	var url = "/roomService";
	var data = { func : "loadFilter"
		}
	var args = {dialog : dialog,id : id}
	simpleserviceWithArgs(url,data,setFilter,args);
}

function setFilter(data,args){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	var roomtype = objArray[0];
	var block = objArray[1];
	var floor = objArray[2];
	var dialog = args["dialog"];
	var rtId = args["id"];
	for ( var i = 0; i < roomtype.length; i++) {
		dialog.find("#searchByRoomType").append(
				$("<option>").val(roomtype[i]['id']).html(roomtype[i]['shortName']));
		}
	for ( var i = 0; i < block.length; i++) {
		dialog.find("#searchByBlock").append(
				$("<option>").val(block[i]['id']).html(block[i]['title']));
		}
	for ( var i = 0; i < floor.length; i++) {
		dialog.find("#searchByFloor").append(
				$("<option>").val(floor[i]['id']).html(floor[i]['title']));
		}
	dialog.find("#searchByRoomType").val(rtId);
	dialog.find("#searchByRoomType").attr("disabled",true);
}